<?php

require_once('src/vo/BaseVO.php');
require_once('src/vo/EventVO.php');
require_once('src/vo/FlagVO.php');
require_once('src/vo/GameStatsVO.php');
require_once('src/vo/GameVO.php');
require_once('src/vo/KillratioStatsVO.php');
require_once('src/vo/PlayerStatsVO.php');
require_once('src/vo/PlayerVO.php');
require_once('src/vo/TotalGameStatsVO.php');

class Cache  {

   public static $ALL_PLAYERS_KEY             = 'all_players';
   public static $ALL_FLAGS_KEY               = 'all_flags';
   public static $ALL_GAMES_KEY               = 'all_games';
   public static $TOTAL_GAMES_COUNT_KEY       = 'total_games_count';
   public static $KILLRATIO_FOR_LAST_GAME_KEY = 'killratio_for_last_game';
   public static $KILLRATIO_FOR_ALL_GAMES_KEY = 'killratio_for_all_games';
   public static $KILLRATIO_FOR_ALL_GAMES_ADJUSTED_KEY = 'killratio_for_all_games_adjusted';
   public static $STATS_FOR_LAST_GAME_KEY     = 'stats_for_last_game';
   public static $STATS_FOR_ALL_GAMES_KEY     = 'stats_for_all_games';
   
   public static $CACHE_TIMES_KEY             = '_cache_times_';
   public static $CACHE_LAST_RECREATION_KEY   = 'last_recreation';   

   public $cacheTimes;
    
   public function __construct() {
        if (file_exists($this->getCacheFileName(Cache::$CACHE_TIMES_KEY))) { 
          $cacheTimesSerialized = implode("", @file($this->getCacheFileName(Cache::$CACHE_TIMES_KEY)));
          $this->cacheTimes = unserialize(base64_decode($cacheTimesSerialized));
        }   
        else {
          $this->cacheTimes = array();
        }
   }
   
   public function isInCache($objectName) {
      return file_exists($this->getCacheFileName($objectName));
   }

   public function getFromCache($objectName) {
     $serialized = implode("", @file($this->getCacheFileName($objectName)));
     $unserialized = unserialize(base64_decode($serialized));
     return $unserialized;
   }
   
   public function putInCache($objectName, $object) {
      $serialized = base64_encode(serialize($object));
      $fp = fopen($this->getCacheFileName($objectName), "w");
      fwrite($fp, $serialized);
      fclose($fp);   
     
      $this->cacheTimes[$objectName] = time();
      $cacheTimesSerialized = base64_encode(serialize($this->cacheTimes));
      $fpc = fopen($this->getCacheFileName(Cache::$CACHE_TIMES_KEY), "w");
      fwrite($fpc, $cacheTimesSerialized);
      fclose($fpc);      
   }      
   
   private function getCacheFileName($objectName) {
      return "cache/".$objectName.".cache";
   }
   
   public function getCacheDate($objectName) {
      return $this->cacheTimes[$objectName];
   }  
   
   public function setCacheRecreationDate($date) {
      $this->cacheTimes[Cache::$CACHE_LAST_RECREATION_KEY] = $date;
      
      $cacheTimesSerialized = base64_encode(serialize($this->cacheTimes));
      $fpc = fopen($this->getCacheFileName(Cache::$CACHE_TIMES_KEY), "w");
      fwrite($fpc, $cacheTimesSerialized);
      fclose($fpc);        
   }            
   
   public function getCacheRecreationDate() {
      return $this->cacheTimes[Cache::$CACHE_LAST_RECREATION_KEY];
   }                   

}
?>
